﻿
function openUrl(url){
	$.get('/islogin.do?t=' + Math.random(), function(data){
		if(data){
	        if (data.success){
	        	//如果有字典销毁他
	        	$('#dataDictionary').remove();
	        	$('#mainpanel').panel('refresh', url);
	        }else{
	        	window.location.href='/';
	        }
    	}
	});
}

 function submitForm(url, redirect){
	var win = $.messager.progress({
		title:'请稍后',
		msg:'数据提交中...'
	});
	$('#form').form({
		url:url,
		type:"POST",
	    success: function(result){
	    	if(result){
	    		var data = eval('(' + result + ')');
		        if (data.success){
		        	if(data.id!=null && data.id!=""){
		        		redirect=redirect+"?id="+data.id;
		        	}
		        	$.messager.progress('close');
		        	$.messager.alert('提示',data.message,'info', function(){
		        		 if(redirect){
		 	            	openUrl(redirect);
		 	            }
		        	});
		        }else{
		        	$.messager.progress('close');
		        	$.messager.alert('提示',data.message,'info');
		        }
	    	}else{
	        	$.messager.progress('close');
	        	$.messager.alert('提示','操作失败!','info');
	        }
	    }
	});
	$('#form').submit(); 
}
 
function submitGetReq(url, redirect){
	var win = $.messager.progress({
		title:'请稍后',
		msg:'数据提交中...'
	});
	$.get(url, function(data){
		if(data){
	        if (data.success){
	        	$.messager.progress('close');
	        	$.messager.alert('提示',data.message,'info', function(){
	        		 if(redirect){
	 	            	openUrl(redirect);
	 	            }
	        	});
	        }else{
	        	$.messager.progress('close');
	        	$.messager.alert('提示',data.message,'info');
	        }
    	}else{
    		$.messager.progress('close');
        	$.messager.alert('提示','操作失败!','info');
        }
	});
}

//打开数据字典  参数依次为:标题、窗口宽、高、回调函数
function openDataDictionary(title, width, height, callback,json){
	$('#dataDictionary').window({
		title:title,
	    width:width,
	    height:height,
	    modal:true
	});
	$('#dataDictionary').window('open');
	if(typeof (callback) == 'function'){
		if(json){
			callback(json);
		}else{
			callback();
		}
	}
}

function closeDataDictionary(callback){
	$('#dataDictionary').window('close');
	if(typeof (callback) == 'function'){
		callback();
	}
}

//回车提交 由document元素触发 target触发提交元素
function enterSubmit(target){
	var objEvt = $._data($(document)[0], "events");
	if (objEvt && objEvt["keyup"]) {
		$(document).unbind("keyup");
	}
	$(document).keyup(function(event){
		if(event.keyCode ==13){
			$("#" + target).trigger("click");
		}
	});
}

//格式化日期 time时间戳 format格式 1 yyyy-MM-dd 2 yyyy-MM-dd HH:mm:ss
function formatDateFromJson(time, format){
	if(time){
		var jsonDateValue = new Date(time);
		var year = jsonDateValue.getFullYear();
		var month = jsonDateValue.getMonth() + 1;
		var day = jsonDateValue.getDate();
		if(format == 1){
			return year + "-" + month + "-" + day;
		}else if(format == 2){
			return year + "-" + month + "-" + day + " " +jsonDateValue.getHours() + ":" + jsonDateValue.getMinutes() + ":" + jsonDateValue.getSeconds();
		}
	}else{
		return "";
	}
}

//编辑Datagrid行
function editDatagrid(){
	var row = $('#datagrid').datagrid('getSelected');
	var index = $('#datagrid').datagrid('getRowIndex', row);
	$('#datagrid').datagrid('beginEdit', index);
}
//保存Datagrid
function saveDatagrid(){
	var row = $('#datagrid').datagrid('getSelected');
	var index = $('#datagrid').datagrid('getRowIndex', row);
	$('#datagrid').datagrid('endEdit', index);
}
//删除Datagrid 行
function delDatagrid(){
	var row = $('#datagrid').datagrid('getSelected');
	var index = $('#datagrid').datagrid('getRowIndex', row);
	$('#datagrid').datagrid('deleteRow', index);
}
